package routers

import (
	"gintest/controllers/c_admin"
	"gintest/controllers/c_store"
	"gintest/middlewares"
	"io"
	"net/http"
	"net/http/httputil"
	"net/url"
	"strings"

	"github.com/gin-gonic/gin"
)

func InitRouter() http.Handler {
	router := gin.New()
	//全局中间件
	router.Use(middlewares.Inits())
	/**
	*后台管理界面
	 */
	g01 := router.Group("/admin.gejinnet.com")
	{
		//加载静态路径
		g01.StaticFS("/", http.Dir("views/site"))
	}
	/**
	* 接口
	 */
	g02 := router.Group("/api.gejinnet.com")
	{
		g02.Use(gin.Recovery())
		//gzip
		//g02.Use(gzip.Gzip(gzip.DefaultCompression))
		//session
		//store := cookie.NewStore([]byte("_secret"))//cookie文件
		//store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("_secret"))//redis
		//store := memstore.NewStore([]byte("_secret"))//内存
		//g02.Use(sessions.Sessions("_session", store))

		//加载静态路径
		//router.Static("/static", "./static")

		g02.POST("/login", c_admin.Login_dologin)
		g02.POST("/upload/index", middlewares.Auth(), c_admin.Upload_index)
		g02.POST("/upload/edit", middlewares.Auth(), c_admin.Upload_edit)
		g02.GET("/upload/img", c_admin.Upload_img)

		//index
		g02.POST("/index/welcome", middlewares.Auth(), c_admin.Index_welcome)
		//config
		g02.POST("/config/list", middlewares.Auth(), c_admin.Config_list)
		g02.POST("/config/add", middlewares.Auth(), c_admin.Config_doadd)
		g02.POST("/config/detail", middlewares.Auth(), c_admin.Config_detail)
		g02.POST("/config/edit", middlewares.Auth(), c_admin.Config_doedit)
		g02.POST("/config/del", middlewares.Auth(), c_admin.Config_del)

		//adpos
		g02.POST("/adpos/list", middlewares.Auth(), c_admin.Adpos_list)
		g02.POST("/adpos/add", middlewares.Auth(), c_admin.Adpos_doadd)
		g02.POST("/adpos/detail", middlewares.Auth(), c_admin.Adpos_detail)
		g02.POST("/adpos/edit", middlewares.Auth(), c_admin.Adpos_doedit)
		g02.POST("/adpos/del", middlewares.Auth(), c_admin.Adpos_del)
		//ad
		g02.POST("/ad/list", middlewares.Auth(), c_admin.Ad_list)
		g02.POST("/ad/add", middlewares.Auth(), c_admin.Ad_doadd)
		g02.POST("/ad/detail", middlewares.Auth(), c_admin.Ad_detail)
		g02.POST("/ad/edit", middlewares.Auth(), c_admin.Ad_doedit)
		g02.POST("/ad/del", middlewares.Auth(), c_admin.Ad_del)
		//userauth
		g02.POST("/authuser/list", middlewares.Auth(), c_admin.AuthUser_list)
		g02.POST("/authuser/add", middlewares.Auth(), c_admin.AuthUser_doadd)
		g02.POST("/authuser/detail", middlewares.Auth(), c_admin.AuthUser_detail)
		g02.POST("/authuser/edit", middlewares.Auth(), c_admin.AuthUser_doedit)
		g02.POST("/authuser/del", middlewares.Auth(), c_admin.AuthUser_del)
		//data.db
		g02.POST("/datadb/list", middlewares.Auth(), c_admin.Datadb_list)
		g02.POST("/datadb/del", middlewares.Auth(), c_admin.Datadb_del)
		//users
		g02.POST("/user/list", middlewares.Auth(), c_admin.User_list)
		g02.POST("/user/add", middlewares.Auth(), c_admin.User_doadd)
		g02.POST("/user/detail", middlewares.Auth(), c_admin.User_detail)
		g02.POST("/user/edit", middlewares.Auth(), c_admin.User_doedit)
		g02.POST("/user/del", middlewares.Auth(), c_admin.User_del)
		g02.POST("/user/password", middlewares.Auth(), c_admin.User_doedit)
		//order
		g02.POST("/order/list", middlewares.Auth(), c_admin.Order_list)
		g02.POST("/order/detail", middlewares.Auth(), c_admin.Order_detail)
		g02.POST("/order/del", middlewares.Auth(), c_admin.Order_del)
		//article class
		g02.POST("/articleclass/list", middlewares.Auth(), c_admin.ArticleClass_list)
		g02.POST("/articleclass/add", middlewares.Auth(), c_admin.ArticleClass_doadd)
		g02.POST("/articleclass/detail", middlewares.Auth(), c_admin.ArticleClass_detail)
		g02.POST("/articleclass/edit", middlewares.Auth(), c_admin.ArticleClass_doedit)
		g02.POST("/articleclass/del", middlewares.Auth(), c_admin.ArticleClass_del)
		//article
		g02.POST("/article/list", middlewares.Auth(), c_admin.Article_list)
		g02.POST("/article/add", middlewares.Auth(), c_admin.Article_doadd)
		g02.POST("/article/detail", middlewares.Auth(), c_admin.Article_detail)
		g02.POST("/article/edit", middlewares.Auth(), c_admin.Article_doedit)
		g02.POST("/article/del", middlewares.Auth(), c_admin.Article_del)
		g02.POST("/article/status", middlewares.Auth(), c_admin.Article_status)
		//delivery
		g02.POST("/delivery/list", middlewares.Auth(), c_admin.Delivery_list)
		g02.POST("/delivery/add", middlewares.Auth(), c_admin.Delivery_doadd)
		g02.POST("/delivery/del", middlewares.Auth(), c_admin.Delivery_del)
		g02.POST("/delivery/detail", middlewares.Auth(), c_admin.Delivery_detail)
		g02.POST("/delivery/edit", middlewares.Auth(), c_admin.Delivery_doedit)
		//store
		g02.POST("/store/list", middlewares.Auth(), c_admin.Store_list)
		g02.POST("/store/add", middlewares.Auth(), c_admin.Store_doadd)
		g02.POST("/store/detail", middlewares.Auth(), c_admin.Store_detail)
		g02.POST("/store/edit", middlewares.Auth(), c_admin.Store_doedit)
		g02.POST("/store/del", middlewares.Auth(), c_admin.Store_del)
		//storeclass
		g02.POST("/storeclass/list", middlewares.Auth(), c_admin.StoreClass_list)
		g02.POST("/storeclass/add", middlewares.Auth(), c_admin.StoreClass_doadd)
		g02.POST("/storeclass/detail", middlewares.Auth(), c_admin.StoreClass_detail)
		g02.POST("/storeclass/edit", middlewares.Auth(), c_admin.StoreClass_doedit)
		g02.POST("/storeclass/del", middlewares.Auth(), c_admin.StoreClass_del)
		//storeuser
		g02.POST("/storeuser/list", middlewares.Auth(), c_admin.StoreUser_list)
		g02.POST("/storeuser/add", middlewares.Auth(), c_admin.StoreUser_doadd)
		g02.POST("/storeuser/detail", middlewares.Auth(), c_admin.StoreUser_detail)
		g02.POST("/storeuser/edit", middlewares.Auth(), c_admin.StoreUser_doedit)
		g02.POST("/storeuser/del", middlewares.Auth(), c_admin.StoreUser_del)
		//storecustomclass
		g02.POST("/storecustomclass/list", middlewares.Auth(), c_admin.StoreCustomClass_list)
		g02.POST("/storecustomclass/add", middlewares.Auth(), c_admin.StoreCustomClass_doadd)
		g02.POST("/storecustomclass/detail", middlewares.Auth(), c_admin.StoreCustomClass_detail)
		g02.POST("/storecustomclass/edit", middlewares.Auth(), c_admin.StoreCustomClass_doedit)
		g02.POST("/storecustomclass/del", middlewares.Auth(), c_admin.StoreCustomClass_del)
		//goodsclass
		g02.POST("/goodsclass/list", middlewares.Auth(), c_admin.GoodsClass_list)
		g02.POST("/goodsclass/add", middlewares.Auth(), c_admin.GoodsClass_doadd)
		g02.POST("/goodsclass/detail", middlewares.Auth(), c_admin.GoodsClass_detail)
		g02.POST("/goodsclass/edit", middlewares.Auth(), c_admin.GoodsClass_doedit)
		g02.POST("/goodsclass/del", middlewares.Auth(), c_admin.GoodsClass_del)
		//goods
		g02.POST("/goods/list", middlewares.Auth(), c_admin.Goods_list)
		g02.POST("/goods/add", middlewares.Auth(), c_admin.Goods_doadd)
		g02.POST("/goods/detail", middlewares.Auth(), c_admin.Goods_detail)
		g02.POST("/goods/edit", middlewares.Auth(), c_admin.Goods_doedit)
		g02.POST("/goods/del", middlewares.Auth(), c_admin.Goods_del)
		g02.POST("/goods/status", middlewares.Auth(), c_admin.Goods_Status)
		g02.POST("/goods/add_attr", middlewares.Auth(), c_admin.GoodsAttr_add)
		g02.POST("/goods/save_attr", middlewares.Auth(), c_admin.GoodsAttr_save)
		g02.POST("/goods/del_attr", middlewares.Auth(), c_admin.GoodsAttr_del)
		g02.POST("/goods/save_spec", middlewares.Auth(), c_admin.GoodsSpec_save)
		//logistics
		g02.POST("/logistics/list", middlewares.Auth(), c_admin.Logistics_list)
		g02.POST("/logistics/add", middlewares.Auth(), c_admin.Logistics_doadd)
		g02.POST("/logistics/detail", middlewares.Auth(), c_admin.Logistics_detail)
		g02.POST("/logistics/edit", middlewares.Auth(), c_admin.Logistics_doedit)
		g02.POST("/logistics/del", middlewares.Auth(), c_admin.Logistics_del)

		//商家接口
		//注册登陆
		g02.POST("/store/ext/register", c_store.External_register)
		g02.POST("/store/ext/getcode", c_store.External_getcode)
		g02.POST("/store/ext/login", c_store.External_login)
		//用户信息
		g02.POST("/store/user/getuserinfo", middlewares.AuthStore(), c_store.User_getuserinfo)
	}
	/**
	*网站1
	 */
	g03 := router.Group("/www.gejinnet.com")
	{
		//加载静态路径
		g03.StaticFS("/", http.Dir("views/site_www"))
	}
	return router
}

/**
8081代理到8082端口
*/
func InitProxy(w http.ResponseWriter, r *http.Request) {
	/*if r.RequestURI == "/favicon.ico" {
		io.WriteString(w, "Request path Error")
		return
	}*/
	hosts := strings.Split(r.Host, ":")
	remoteHost := "http://127.0.0.1:8082"
	if len(hosts) >= 1 {
		remoteHost += "/" + hosts[0]
	} else {
		io.WriteString(w, "Request path Error")
		return
	}
	//fmt.Println(remoteHost)
	remote, err := url.Parse(remoteHost) //("http://" + "127.0.0.1:8082")
	if err != nil {
		panic(err)
	}
	proxy := httputil.NewSingleHostReverseProxy(remote)
	proxy.ServeHTTP(w, r)
}

/*80端口重定向到443端口*/
func Redirect(w http.ResponseWriter, req *http.Request) {

	target := "https://" + req.Host + req.URL.Path
	if len(req.URL.RawQuery) > 0 {
		target += "?" + req.URL.RawQuery
	}

	http.Redirect(w, req, target, http.StatusTemporaryRedirect)
}
